package com.hanson.service;

import com.hanson.common.util.BusinessUtil;
import com.hanson.entity.GoodsBean;
import com.hanson.entity.GoodsCollectBean;
import com.hanson.mapper.GoodsBeanMapper;
import com.hanson.mapper.GoodsCollectBeanMapper;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class GoodsCollectService {
    @Autowired
    private GoodsCollectBeanMapper goodsCollectBeanMapper;
    @Autowired
    private GoodsBeanMapper goodsBeanMapper;

    public void add(String goodsId, String openId) {
        GoodsCollectBean goodsCollectBean = new GoodsCollectBean();
        goodsCollectBean.setGoodsId(Integer.parseInt(goodsId));
        goodsCollectBean.setCreateDate(new Date());
        goodsCollectBean.setOpenId(openId);
        goodsCollectBeanMapper.insert(goodsCollectBean);
    }

    public List<GoodsBean> selectByOpenId(String openId) {
        List<GoodsCollectBean> goodsCollectBeanList = goodsCollectBeanMapper.selectByOpenId(openId);
        List<GoodsBean> returnList = new ArrayList<>();
        if (CollectionUtils.isNotEmpty(goodsCollectBeanList)) {
            for (GoodsCollectBean goodsCollectBean : goodsCollectBeanList) {
                GoodsBean goodsBean = goodsBeanMapper.selectByPrimaryKey(goodsCollectBean.getGoodsId());
                if (null != goodsBean) {
                    goodsBean.setWebPrice(BusinessUtil.getWebAmount(goodsBean.getPrice()));
                    goodsBean.setWebVipPrice(BusinessUtil.getWebAmount(goodsBean.getVipPrice()));
                    returnList.add(goodsBean);
                }
            }
        }
        return returnList;
    }

    public void delete(String openId, Integer goodsId) {
        goodsCollectBeanMapper.deleteByOpenIdAndGoodsId(openId, goodsId);
    }

    public int count(String openId, Integer goodsId) {
        return goodsCollectBeanMapper.countByOenIdAndGoodsId(openId, goodsId);
    }
}
